package com.daisuxia.rongbao.utils;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取config.properties配置
 * @author LTQ
 *
 */
public class PropertiesUtil {

	private static Properties properties = new Properties();

	private static WebApplicationContext webApplicationContext;
	static {
		webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		//目前测试
		InputStream in = PropertiesUtil.class.getResourceAsStream("/CER_rongbao_real/app_real.properties");
		try {
			properties.load(in); 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    public static String get(String key) {
    	
    	String value = properties.getProperty(key);
    	if(value != null){
    		value = value.trim();
    	}
    	if("privateKey".equals(key)||"pubKeyUrl".equals(key)){
			if(webApplicationContext==null){
				value = PropertiesUtil.class.getResource(value).getPath();
			}else{
				value = webApplicationContext.getServletContext().getRealPath("/WEB-INF/classes/"+value);
			}
    	}
    	return value;
    }  
}
